package com.itheima.mm.controller;

import com.itheima.mm.dto.PasswordDto;
import com.itheima.mm.dto.UserinfoDto;
import com.itheima.mm.pojo.BsUser;
import com.itheima.mm.dto.Login;
import com.itheima.mm.service.LoginService;
import org.apache.ibatis.annotations.Options;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.UUID;
@RestController
@RequestMapping("/frame")
public class LoginController {
    @Autowired
    private LoginService loginService;

    @Autowired
    private HttpServletRequest req;

    //TODO 登录接口
    @PostMapping("/login")
    public String login(Login login) {
        String  token = UUID.randomUUID().toString();
        if (login == null) {
            return token = "参数错误~~";
        }

        BsUser bsUser2 = loginService.login(login);
        System.err.println(bsUser2);
        if (bsUser2==null){
            return token = "用户名或密码错误~~";
        }
        HttpSession session = req.getSession();
        session.setAttribute("id",bsUser2.getId());
        return token ;
    }

    //TODO 退出登录接口
    @PostMapping("/logout")
    public String logout() {
        HttpSession session = req.getSession();
        session.invalidate();
        return "退出成功";
    }


    //TODO 注册接口
    @PostMapping("/register")
    public String register(@RequestBody Login login){
       return loginService.register(login);
    }


    //TODO 修改密码操作接口
    @PostMapping("/password")
    public String revampPassword(@RequestBody PasswordDto passwordDto){
      return loginService.revampPassword(passwordDto);
    }
    //TODO 查询用户资料
    @PostMapping("/profile")
    public UserinfoDto profile(){
//        return "请先登录";
       return loginService.profile();
    }
}
